var procedure = [];
var switc = false;
var status = false;
var resolvePromise;
function lockPromise() {
	return new Promise(function promiseExecutor(resolve) {
		resolvePromise = resolve;
	});
}
function PromiseThen(fn) {
	new Promise(function (reslove) {
		reslove();
	}).then(function () {
		fn();
	});
}

function setStorage() {
	chrome.storage.sync.set({ procedure: procedure }, function () {
		console.log(procedure);
	});
}
function getStorage() {
	chrome.storage.sync.get("procedure", function (result) {
		procedure = result.procedure;
	});
}

chrome.storage.sync.get("procedure", function (result) {
	procedure = result.procedure;
});
chrome.storage.sync.get("status", function (result) {
	status = result.status;
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
	if (request.cmd == "start") {
		chrome.storage.sync.set({ status: 0 });
		status = 1;
	}
	if (request.cmd == "end") {
		chrome.storage.sync.set({ status: 0 });
		status = 0;
	}
	if (request.cmd == "reset") {
		status = 0;
		chrome.storage.sync.set({ status: 0 });
		getStorage();
	}
	sendResponse();
});

chrome.runtime.onMessage.addListener(function (res, sender, sendResponse) {
	if (res.cycle === "beforeSendHeaders") {
		beforeSendHeaders(res);
	}
	if (res.cycle === "completed") {
		completed(res);
	}
	sendResponse();
});
let timer = null;
function beforeSendHeaders(res) {
	let url = res.url.replace(res.initiator, "");

	if (switc && url.indexOf(".") === -1 && url.indexOf("http") == -1 && procedure.indexOf(url) == -1) {
		clearTimeout(timer);
		resolvePromise && resolvePromise(true);
		PromiseThen(function () {
			if (procedure.indexOf(url) == -1) {
				procedure.push(url);
				setStorage();
			}
		});
	}
}
function completed(res) {
	if (switc) {
		closeEvent();
	}
}
function closeEvent() {
	timer = setTimeout(function () {
		switc = false;
	}, 500);
}

function mousedownFn() {
	if (status == 1) {
		switc = true;
		lockPromise().then(function (res) {
			if (res) {
				procedure.push("click");
				setStorage();
			}
		});
	}
}

window.onload = function () {
	if (status == 1) {
		switc = true;
		procedure.push(window.location.href);
		closeEvent();
	}

	iframeEvent();
	// 按下
	document.addEventListener("mousedown", mousedownFn);
	// 抬起
	document.addEventListener("mouseup", function (e) {
		if (switc) {
			closeEvent();
		}
	});

	// 当观察到突变时执行的回调函数
	var callback = function (mutationsList) {
		mutationsList.forEach(function (item, index) {
			if (item.type == "childList") {
				iframeEvent();
			} else if (item.type == "attributes") {
				//	console.log("修改了" + item.attributeName + "属性");
			}
		});
	};
	var observer = new MutationObserver(callback);
	observer.observe(document.body, { childList: true });

	function iframeEvent() {
		var iframes = document.getElementsByTagName("iframe");
		for (let i = 0; i < iframes.length; i++) {
			if (!iframes[i].onload) {
				iframes[i].onload = function (e) {
					evnet(iframes[i]);
				};
				evnet(iframes[i]);
			}
		}
	}

	function evnet(iframe) {
		iframe.contentDocument.addEventListener("mousedown", mousedownFn);
		iframe.contentDocument.addEventListener("mouseup", function () {
			if (switc) {
				closeEvent();
			}
		});
	}
};
